import { initSearchConfig, TableSearchPanel, XxpageContainer, XxTable } from "@/ui";
import React, { useEffect, useRef, useState } from "react";
import searchFields from "./config/searchFields";
import getActoinPanel from "./config/action";
import getColumns from "./config/columns";
import EditForm from "./editForm";
import { subjectApi as api } from "@/api";
import { globalModal } from "@/data";
import { deleteConfirm, successAlert } from "@/common/methods";

/**
 * 说明：
 * TableSearchPanel 搜索面板组件，initialValues可以传入默认值，


 */


/**@description 专题列表页面 */
const SubjectListView = () => {
    //设置搜索面板默认值
    const onSearchRef = useRef(null);
    const editFormRef = useRef(null);
    const [params, setParams] = useState({ ...initSearchConfig(searchFields) })
    const [state, setState] = useState({
        data: [],
        totalCount: 0
    })
    const [loading, setLoading] = useState(false);

    //搜索事件
    const onClickSearch = (value) => {
        setParams({
            ...params,
            ...value,
            current: 1,
        });
    };


    /**@description 查找列表 */
    const getList = async () => {
        setLoading(true);
        const { data, code, msg } = await api.queryList({
            pageSize: 10,
            current: 1,
            ...params,
            ...onSearchRef.current.getFormDatas()
        });
        setState({
            data: [...data.list],
            totalCount: data.totalCount || 0,
        })
        setLoading(false);

    }

    const onChange = (current, pageSize) => {
        setParams({
            ...params,
            current,
            pageSize
        })
    }
    useEffect(() => {
        getList()
    }, [params])

    /**  @description 打开编辑菜单窗口 */
    const open = ({ type = "add", record = Object }) => {
        editFormRef.current.open({ type, record });
    }

    /**@description 删除按钮操作 */
    const handleDelete = async (object) => {
        try {
            await deleteConfirm();
            const { code, msg } = await api.delete(object.id)
        } catch (error) {

        } finally {
            getList()
        }
    }


    const [selectedKeys, setSelectedKeys] = useState([])
    const onSelectChange = (newSelectedRowKeys, selectedRows) => {
        setSelectedKeys(newSelectedRowKeys);
    }
    const actionPanel = getActoinPanel({
        open: open
    })
    const tableProps = {
        tablePanelConfig: actionPanel,
        loading,
        columns: getColumns({ open, handleDelete }),
        dataSource: state.data,
        pagination: {
            total: state.totalCount,
            pageSize: params.pageSize,
            onChange: onChange,
            current: params.current

        },
        rowSelection: {
            type: "checkbox",
            selectedRowKeys: selectedKeys,
            onChange: onSelectChange,
        },
        rowKey: 'id',
        selectedRowKeys: selectedKeys,
        onRow: (record, index) => {
            return {
                onDoubleClick: () => {

                }
            }
        },
    }


    return (
        <XxpageContainer>
            <EditForm onRef={editFormRef} getList={getList} />
            <TableSearchPanel
                // initialValues={{
                //     //设置默认值
                //     ...(() => {
                //         let obj = {};
                //         searchFields.map(item => {
                //             obj[item.name] = item.initValue || undefined
                //         })
                //         return obj;
                //     })()
                // }}
                onRef={onSearchRef}
                fields={searchFields}
                onSearch={onClickSearch}

            />
            <XxTable  {...tableProps} />

        </XxpageContainer>
    )
}

export default SubjectListView;